package com.henry.chatkit.utils

import android.content.pm.ActivityInfo
import androidx.fragment.app.Fragment
import com.henry.chatkit.R
import com.henry.chatkit.glide.PictureGlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener


object ChatTool {

    fun openPhoto(fragment: Fragment, maxCount: Int, callback: (List<LocalMedia?>) -> Unit) {
        PictureSelector.create(fragment)
            .openGallery(PictureMimeType.ofImage())
            .imageEngine(PictureGlideEngine.createGlideEngine())
            .isWeChatStyle(true)// 是否开启微信图片选择风格
            .imageSpanCount(4)// 每行显示个数
            .isCamera(false)// 是否显示拍照按钮
            .maxSelectNum(maxCount)
            .isCompress(true)
            .selectionMode(PictureConfig.SINGLE)
            .isSingleDirectReturn(true)
            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)// 设置相册Activity方向，不设置默认使用系统
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: List<LocalMedia?>) {
                    callback.invoke(result)
                }

                override fun onCancel() {
                }
            })

    }

    fun openCamera(fragment: Fragment, callback: (List<LocalMedia?>) -> Unit) {
        // 单独拍照
        PictureSelector.create(fragment)
            .openCamera(PictureMimeType.ofImage())
            .imageEngine(PictureGlideEngine.createGlideEngine())
            .isCompress(true)
            .forResult(object : OnResultCallbackListener<LocalMedia?> {
                override fun onResult(result: List<LocalMedia?>) {
                    callback.invoke(result)
                }

                override fun onCancel() {
                }
            })
    }

    fun openPreview(position: Int, selectList: List<LocalMedia>) {

        PictureSelector.create(appActivity())
            .themeStyle(R.style.picture_default_style) // xml设置主题
            .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)// 设置相册Activity方向，不设置默认使用系统
            .isNotPreviewDownload(true)// 预览图片长按是否可以下载
            //.bindCustomPlayVideoCallback(new MyVideoSelectedPlayCallback(getContext()))// 自定义播放回调控制，用户可以使用自己的视频播放界面
            .imageEngine(PictureGlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
            .openExternalPreview(position, selectList);
    }
}